package com.greenant.store.fragment;

import java.util.ArrayList;
import java.util.List;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.greenant.store.R;
import com.greenant.store.adapter.GoodsStylesAdapter;
import com.greenant.store.bean.GoodsStyles;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class GoodsStylesFragment extends Fragment {
    private static final String TAG = "GoodsStyleFragment";
    private Handler handler=null;
    private ListView lv;
    static GoodsStylesFragment newInstance(String s) {
        GoodsStylesFragment newFragment = new GoodsStylesFragment();
        Bundle bundle = new Bundle();
        bundle.putString("paraStyle", s);
        newFragment.setArguments(bundle);
        
      /*  final SerializableMap myMap=new SerializableMap();
        myMap.setMap(map);
        Bundle bundle = new Bundle();
        bundle.putSerializable("map", myMap);
        newFragment.setArguments(bundle);*/
        return newFragment;       
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       /* Bundle bundle = getArguments();
        SerializableMap serializableMap = (SerializableMap) bundle.get("map");
        maplist =serializableMap.getMap();*/
        Bundle bundle = getArguments();
        getGoodsStyleList(bundle.getString("paraStyle"));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        
        View view = inflater.inflate(R.layout.lay_goods_styles, container, false);
        lv  = (ListView) view.findViewById(R.id.listStyles);         
             
        try 
        {
            //handler与线程之间的通信及数据处�?
            handler=new Handler()
            {
                public void handleMessage(Message msg) 
                {
                    if(msg.what==1)
                    {
                        //msg.obj是获取handler发�?�信息传来的数据
                        @SuppressWarnings("unchecked")
                        ArrayList<GoodsStyles> list=(ArrayList<GoodsStyles>) msg.obj;
                        //给ListView绑定数据
                        BinderListData(list);
                    }
                }
            };
        }catch (Exception e){
            e.printStackTrace();
        } 
        return view;

    }
  //绑定数据
    private void BinderListData(ArrayList<GoodsStyles> list)
    {
    	 GoodsStylesAdapter hc = GoodsStylesAdapter.newInstance(getActivity().getApplicationContext(),list);
         lv.setAdapter(hc);
         lv.setCacheColorHint(0);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    private void getGoodsStyleList(String str){
		AVQuery<AVObject> query=AVQuery.getQuery("AVGoodsStyle");
		query.orderByDescending("updateAt");		
		// 限制�?1000条�??
		query.setLimit(1000);
		query.setCachePolicy(AVQuery.CachePolicy.CACHE_ELSE_NETWORK );
		query.findInBackground(new FindCallback<AVObject>() {			 
		   public void done(List<AVObject> styleList, AVException e) {
			   ArrayList<GoodsStyles> list =new ArrayList<GoodsStyles>();
			   for (AVObject style : styleList) {			        		         
			        GoodsStyles goodsStyle = new GoodsStyles();		       
		        	goodsStyle.setStyles(style.getString("name"));
		        	//goodsStyle.setCount("�?"+"件商�?");
		        	list.add(goodsStyle);
			     }
		     handler.sendMessage(handler.obtainMessage(1, list));
		   }
		 });
	}

}
